package org.jeecgframework.web.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.UploadOrgService;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.ExcelWrite;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.jeecgframework.web.util.UploadFileUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

/**   
 * @Title: UploadOrgServiceImpl
 * @Description: 上传影院信息
 * @author ZhangYunlong
 * @date 2016-04-01
 * @version V1.0   
 *
 */
@Service("uploadOrgService")
@Transactional
public class UploadOrgServiceImpl extends CommonServiceImpl implements
		UploadOrgService{

	/**
	 * @Description 解析EXCEL
	 * @param HttpServletRequest 通过前台请求获得文件流
	 */
	public List<TSDepart> parseExcel(HttpServletRequest req) {
		List<TSDepart> excelList = new ArrayList<TSDepart>();
		XSSFWorkbook hwk = null;
		/*ZyFileLogEntity fileLog  = new ZyFileLogEntity();
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		
		String fileName = UUID.randomUUID().toString().replace("-", "")+".xlsx";
		try {
			for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
				MultipartFile file = entity.getValue();// 获取上传文件对象
				hwk = ExcelUtil.analyzeExcel2(file.getInputStream());
				fileLog.setBusinessType(StaticValue.IMPORT_FILE_ORG_BASE_DATA); //文件业务
				fileLog.setOldFileName(file.getOriginalFilename());//文件原名
				fileLog.setNewFileName(fileName);//文件新名
				fileLog.setFilePath(StaticValue.UPLAOD_FILE_PATH+fileName);
				fileLog.setFileType("xlsx");
				fileLog.setStatus("0");//文件新名
				ExcelWrite.createExcel(hwk,fileName);
				super.save(fileLog);
				excelList = parseExcelToList(hwk);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return excelList;*/
		
		FileBean fileBean = UploadFileUtil.UploadFile(req);
		try {
			ZyFileLogEntity fileLog  = new ZyFileLogEntity();
			fileLog.setBusinessType(StaticValue.IMPORT_FILE_ORG_BASE_DATA); //文件业务
			fileLog.setOldFileName(fileBean.getFileRealName());//文件原名
			fileLog.setNewFileName(fileBean.getFileName());//文件新名
			fileLog.setFilePath(fileBean.getSavePath());
			fileLog.setFileType("xlsx");
			fileLog.setStatus("0");//文件新名
			super.save(fileLog);
			hwk = ExcelUtil.analyzeExcel2(new FileInputStream(new File(fileBean.getSavePath())));
			excelList = parseExcelToList(hwk);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return excelList;
	}

	/**
	 * @Description 将通过POI解析出来的EXCEL转换成对应的实体LIST
	 * @param XSSFWorkbook Excel2007+对象
	 */
	private List<TSDepart> parseExcelToList(XSSFWorkbook hwb) {

		List<TSDepart> result = new ArrayList<TSDepart>();
		// 得到第一个工作表
		XSSFSheet sheet = hwb.getSheetAt(0);
		XSSFRow row = null;
		
		// 遍历该表格中所有的工作表，i表示工作表的数量 getNumberOfSheets表示工作表的总数
		for (int i = 0; i < hwb.getNumberOfSheets(); i++) {
			sheet = hwb.getSheetAt(i);
			TSDepart zyOrg;
			// 遍历该行所有的行,j表示行数 getPhysicalNumberOfRows行的总数
			for (int j = 1; j < sheet.getPhysicalNumberOfRows(); j++) {
				row = sheet.getRow(j);
				zyOrg = new TSDepart();
				zyOrg.setDepartname(ExcelUtil.getCellValue(row.getCell(0)));
				zyOrg.setShortname(ExcelUtil.getCellValue(row.getCell(1)));
				zyOrg.setOrgType(ExcelUtil.getCellValue(row.getCell(2)));
				zyOrg.setAddress(ExcelUtil.getCellValue(row.getCell(3)));
				zyOrg.setPhone(ExcelUtil.getCellValue(row.getCell(5)));
				zyOrg.setFaxCode(ExcelUtil.getCellValue(row.getCell(6)));
				zyOrg.setBpCode(ExcelUtil.getCellValue(row.getCell(7)));
				zyOrg.setLogicDelete("0");
				result.add(zyOrg);
			}
		}
		return result;
	}

	/**
	 * @Description 批量保存
	 * @param List<T>
	 */
	public <T> void batchSave(List<T> entitys) {
		for (T t : entitys) {
			super.save(t);
		}
	}
}
